package cn.edu.kzjsxy.graduationproject.service.impl;

import cn.edu.kzjsxy.graduationproject.common.Page;
import cn.edu.kzjsxy.graduationproject.common.Result;
import cn.edu.kzjsxy.graduationproject.common.TokenUtil;
import cn.edu.kzjsxy.graduationproject.dto.DormitoryDTO;
import cn.edu.kzjsxy.graduationproject.dto.StudentDormitoryAssignment;
import cn.edu.kzjsxy.graduationproject.entity.StudentDormAssignments;
import cn.edu.kzjsxy.graduationproject.entity.Units;
import cn.edu.kzjsxy.graduationproject.entity.Users;
import cn.edu.kzjsxy.graduationproject.mapper.StudentDormAssignmentsMapper;
import cn.edu.kzjsxy.graduationproject.service.IStudentDormAssignmentsService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 学生宿舍分配表 服务实现类
 * </p>
 *
 * @author chén jiā jùn
 * @since 2024-04-14
 */
@Service
public class StudentDormAssignmentsServiceImpl extends ServiceImpl<StudentDormAssignmentsMapper, StudentDormAssignments> implements IStudentDormAssignmentsService {

	@Autowired
	StudentDormAssignmentsMapper studentDormAssignmentsMapper;

	//	tokenUitl
	@Autowired
	private TokenUtil tokenUtil;

//	根据条件查询学生宿舍分配信息
	@Override
	public List<StudentDormAssignments> selectStudentDormAssignmentsByCondition(StudentDormAssignments studentDormAssignments) {
		List<StudentDormAssignments> studentDormAssignmentsList = studentDormAssignmentsMapper.selectStudentDormAssignmentsByCondition(studentDormAssignments);
		return studentDormAssignmentsList;
	}


//	分页获取学生宿舍分配信息
	@Override
	public Result<PageInfo<StudentDormAssignments>> selectStudentDormAssignmentsByPage(Page page, StudentDormAssignments studentDormAssignments) {
		// 设置分页
		PageHelper.startPage(page.getPageNum(), page.getPageSize());
		// 执行查询
		List<StudentDormAssignments> studentDormAssignmentsList = studentDormAssignmentsMapper.selectStudentDormAssignmentsByCondition(studentDormAssignments);
		PageInfo<StudentDormAssignments> pageInfo = new PageInfo<>(studentDormAssignmentsList);
		return Result.success(pageInfo);
	}


	//	查询学生宿舍分配信息 (多表查询)
	@Override
	public List<StudentDormitoryAssignment> getStudentDormAssignments(StudentDormitoryAssignment studentDormitoryAssignment) {
		List<StudentDormitoryAssignment> studentDormAssignmentsList = studentDormAssignmentsMapper.getStudentDormAssignments(studentDormitoryAssignment);
		//      分页查询的总数大于0
		if(studentDormAssignmentsList!=null&&!studentDormAssignmentsList.isEmpty()){
			for (StudentDormitoryAssignment dto : studentDormAssignmentsList) {
//				解析日期
				if(dto.getAllocationDate()!=null){
					dto.setAllocationDate2(formatLocalDateTimeToYMD(dto.getAllocationDate()));
				}
				if(dto.getCheckOutDate()!=null){
					dto.setCheckOutDate2(formatLocalDateTimeToYMD(dto.getCheckOutDate()));
 				}

				List<StudentDormAssignments> studentDormAssignments = this.list(new LambdaQueryWrapper<StudentDormAssignments>().eq(StudentDormAssignments::getDormitoryId, dto.getDormitoryId()));
				dto.setAssigned(studentDormAssignments.size()+"/"+dto.getDormitoryBed());
			}
		}
		return studentDormAssignmentsList;
	}


	//	分页查询学生宿舍分配信息 (多表查询)
	@Override
	public Result<PageInfo<StudentDormitoryAssignment>> getStudentDormAssignmentsPage(Page page, StudentDormitoryAssignment studentDormitoryAssignment, HttpServletRequest request) {
		Users user = tokenUtil.resolveToken(request);
		if(Objects.equals(user.getRole(), "0")){
			studentDormitoryAssignment.setHousemasterId(user.getUserId());
		}
		// 设置分页
		PageHelper.startPage(page.getPageNum(), page.getPageSize());
		// 执行查询
		List<StudentDormitoryAssignment> studentDormAssignmentsList = studentDormAssignmentsMapper.getStudentDormAssignments(studentDormitoryAssignment);
		PageInfo<StudentDormitoryAssignment> pageInfo = new PageInfo<>(studentDormAssignmentsList);
		//      分页查询的总数大于0
		if(pageInfo.getTotal()>0){
			List<StudentDormitoryAssignment> list = pageInfo.getList();
			for (StudentDormitoryAssignment dto : list) {
				List<StudentDormAssignments> studentDormAssignments = this.list(new LambdaQueryWrapper<StudentDormAssignments>().eq(StudentDormAssignments::getDormitoryId, dto.getDormitoryId()));
				dto.setAssigned(studentDormAssignments.size()+"/"+dto.getDormitoryBed());
			}
			pageInfo.setList(list);
		}

		return Result.success(pageInfo);
	}

//  查询未分配的学生信息（多表查询）
	public List<Users> getStudentsWithoutDormitory(){
		List<Users> studentsWithoutDormitory = studentDormAssignmentsMapper.getStudentsWithoutDormitory();
		return studentsWithoutDormitory;
	}


	// 转换为年月日形式的通用方法
	public String formatLocalDateTimeToYMD(LocalDateTime localDateTime) {
		DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
		return localDateTime.format(formatter);
	}
}
